outputfolder=./transfered/
logsfolder=./logs/
gridsfolder=./grids/

movegrids=true
movelogs=true
movesource=true
moveresults=true

for iarg in $*; do
    CLIinputs[$argCounter]=$iarg
    option=`echo "$iarg" | cut -d"=" -f1`
    if [ "$option" == "--nomoveresults" ]; then
	moveresults=false
    fi	
    if [ "$option" == "--nomovesource" ]; then
	movesource=false
    fi
    
    if [ "$option" == "-h" ]; then
	echo "hmerge tt_bbl_todk_mstw8nl_173_173_LHC8_mt173_pt40r5_484873"
	echo "take a bunch of .C root histograms with name tt_bbl_todk_mstw8nl_173_173_LHC8_mt173_pt40r5_484873* and convert them to .root, then merge them in a single.root file, and finally ouput each histogram named id# into a  x-y file .hist"
       	exit
    fi
    let argCounter=argCounter+1
done

loopydo.sh "ls $1*.C" 'root -q -l -b'	
hadd -f $1.root `find $1_*.root -type f -print`
root2hist.sh $1.root

if [ ! -d "$outputfolder" ]; then
    mkdir $outputfolder
fi
#
tar -zcvf $1.hist.tar.gz *.hist
if [ "$moveresults" = true ]; then
    mv -v *.hist $outputfolder
fi
#
tar -zcvf $1.root.tar.gz $1_*.root
if [ "$moveresults" = true ]; then
    mv -v $1_*.root $outputfolder
fi
#

if [ "$movesource" = true ]; then
    mv -v $1* $outputfolder
fi

if [ ! -d "$logsfolder" ]; then
mkdir $logsfolder
fi

if [ "$movelogs" = true ]; then
clusterid=`echo "$1" | rev | cut -d"_" -f1 | rev`
echo "moving logs for cluster ID "$clusterid
mv -v *$clusterid*.stdout $logsfolder
mv -v *$clusterid*.log $logsfolder
mv -v *$clusterid*.stderr $logsfolder
fi

#if [ ! -d "$gridsfolder" ]; then
#mkdir $gridsfolder
#fi

#if [ "$movegrids" = true ]; then
#clusterid=`echo "$1" | rev | cut -d"_" -f1 | rev`
#echo "moving gridss for cluster ID "$clusterid
#mv -v *$clusterid*.grid $gridsfolder
#fi
